package com.mingqijia.gassafety.job.hangzhou.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mingqijia.gassafety.db.entity.DeviceState;
import com.mingqijia.gassafety.db.hangzhou.entity.DeviceDataReport;
import com.mingqijia.gassafety.db.hangzhou.entity.ThingModelProperty;
import com.mingqijia.gassafety.db.hangzhou.mapper.CityDeviceMapper;
import com.mingqijia.gassafety.db.hangzhou.mapper.DeviceDataReportMapper;
import com.mingqijia.gassafety.db.hangzhou.mapper.ThingModelOutputDataMapper;
import com.mingqijia.gassafety.db.hangzhou.mapper.ThingModelPropertyMapper;
import com.mingqijia.gassafety.db.influxdb.config.InfluxDBConfiguration;
import com.mingqijia.gassafety.db.mapper.DeviceStateMapper;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import com.mingqijia.gassafety.job.hangzhou.request.CityDeviceDataGeneralRequest;
import com.mingqijia.gassafety.job.hangzhou.request.SetCityDeviceData;
import com.mingqijia.gassafety.job.hangzhou.response.HzResponse;
import com.mingqijia.gassafety.job.request.ConsumerListRequest;
import com.mingqijia.gassafety.job.response.ConsumerResponse;
import com.mingqijia.gassafety.job.service.ConsumerService;
import com.mingqijia.gassafety.shared.constant.EquipmentTypeConst;
import com.mingqijia.gassafety.shared.utils.RedisCacheExt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName setCityDeviceDataServiceImpl.java
 * @createTime 2022年05月11日
 */
@Service
@Slf4j
public class SetCityDeviceDataServiceImpl {

    @Value("${gassafety.hangzhou.yhSpId}")
    private String hangzhouSpId;
    @Autowired
    RedisCacheExt redisUtil;
    @Autowired
    HangZhouServiceImpl hangZhouService;
    @Autowired
    CityDeviceMapper cityDeviceMapper;
    @Autowired
    ThingModelPropertyMapper propertyMapper;
    @Autowired
    ThingModelOutputDataMapper outputDataMapper;
    @Autowired
    ConsumerService consumerService;
    @Autowired
    InfluxDBConfiguration influxDBConnect;
    @Autowired
    DeviceDataReportMapper deviceDataReportMapper;
    @Autowired
    DeviceStateMapper deviceStateMapper;
    @Autowired
    DtuMapper dtuMapper;

    public boolean execute() {
        //查询余杭设备最新状态
        List<DeviceState> dataList = deviceStateMapper.selectNewestList(hangzhouSpId);

        List<String> imeis = dataList.stream().map(DeviceState::getDeviceId).collect(Collectors.toList());

        //关联查询
        log.info("setCityDeviceGeneralDataJob-需要上报的设备：{}", JSONObject.toJSONString(imeis));
        if (CollectionUtil.isEmpty(imeis)) {
            return true;
        }
        //获取设备基本信息
        //获取设备及对应客户信息
        List<Map<String, Object>> list= dtuMapper.selectRelation(imeis);
        if (CollectionUtil.isEmpty(list)) {
            return true;
        }

        //获取 对应客户信息
        for (Map<String, Object> map : list) {
            Integer consumerId = StringUtils.isEmpty(map.get("ConsumerId"))
                    ? 0 : Integer.parseInt(map.get("ConsumerId") + "");
            String spId = StringUtils.isEmpty(map.get("tSpId"))
                    ? "-" : map.get("tSpId") + "";
            // 获取 设备dtu对应的客户信息
            ConsumerListRequest consumerListRequest = new ConsumerListRequest();
            List<Integer> consumerIds = new ArrayList<>();
            consumerIds.add(consumerId);
            consumerListRequest.setConsumerIdList(consumerIds);
            consumerListRequest.setSpId(spId);
            List<ConsumerResponse> consumerList = consumerService.consumerList(consumerListRequest).getList();
            ConsumerResponse consumerResponse = consumerList.get(0) == null ? new ConsumerResponse() : consumerList.get(0);
            ConsumerResponse consumer = new ConsumerResponse();
            consumer.setId(consumerResponse.getId());
            consumer.setContactsName(consumerResponse.getContactsName());
            consumer.setContactsTelephone(consumerResponse.getContactsTelephone());

            //构造入参
            CityDeviceDataGeneralRequest request = new CityDeviceDataGeneralRequest();
            request.setConsumerResponse(consumer);
            request.setMap(map);
            for (DeviceState deviceState : dataList) {
                if (deviceState.getDeviceId().equals(String.valueOf(map.get("iMEI")))){
                    request.setDeviceState(deviceState);
                }
            }
            //发起推送
            hangZhouService.setCityDeviceData(request);
            log.info("设备:{},普通推送完成",map.get("iMEI"));
        }
        return true;
    }



}